#!/usr/bin/perl 
#use lib "lib/";
#use lib "lib";
use MARC::Record;
use MARC::Batch;
use utf8;

$file = $ARGV[0] or die "no filename\n";

# Use Marc lib using the file specified above
my $batch = MARC::Batch->new('USMARC', $file);

$batch->strict_off();

sub processrecord { 
  # try to do MARC::Record, throw an exception if not
  eval {

    while (my $record = $batch->next()) {
        #$record->encoding( 'UTF-8' );
        print "Encoding " . $record->encoding() . " \n";
        print "Record: $recordcount \n";
        #my $leader = $record->leader();
        #my @fields = $record->fields();
        my $prettyprint = $record->as_formatted();
        print $prettyprint;
        print "\n\n";
        $recordcount++;
    }
    1;
  } or do {
    print "ERROR: couldn't do it $recordcount \n";
    print "Warning " . $batch->warnings() . "\n";
    processrecord();
  }
}

# now call the sub, which tries a eval, which has a while loop, and when it goes wrong the sub calls itself.
# this is call bad perl.
processrecord();

print "End of file. $recordcount records \n";
close(OUT);
